using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Threading;

namespace RapidHDL
{
        //http://www.codeproject.com/KB/cs/highperformancetimercshar.aspx

        public class Timer
        {
            [DllImport("Kernel32.dll")]
            private static extern bool QueryPerformanceCounter(
                out long lpPerformanceCount);

            [DllImport("Kernel32.dll")]
            private static extern bool QueryPerformanceFrequency(
                out long lpFrequency);

            private long lStart, lEnd;
            private long lFrequency;
            private bool bStop;

            // Constructor

            public Timer()
            {
                lStart = 0;
                lEnd = 0;
                bStop = true;

                if (QueryPerformanceFrequency(out lFrequency) == false)
                {
                    // high-performance counter not supported

                    throw new Win32Exception();
                }
            }

            // Start the timer

            public void Start()
            {
                // lets do the waiting threads there work

                Thread.Sleep(0);

                QueryPerformanceCounter(out lStart);
                bStop = false;
            }

            // Stop the timer

            public void Stop()
            {
                QueryPerformanceCounter(out lEnd);
                bStop = true;
            }

            // Returns the duration of the timer (in seconds)

            public double Duration
            {
                get
                {
                    if (!bStop)
                        QueryPerformanceCounter(out lEnd);

                    return (double)(lEnd - lStart) / (double)lFrequency;
                }
            }
        }
    }

